/**
 * 
 */
package com.net.service;

import org.springframework.beans.factory.annotation.Autowired;

import com.net.dao.UserDao;
import com.net.domain.Staff;
import com.net.domain.Student;
import com.net.domain.User;
import com.net.domain.UserType;
import com.net.web.form.LoginForm;

/**
 * @author Dinesh
 *
 */
public class UserServiceImpl implements UserService {

	private UserDao userDao;
	
	private StudentService studentService;
	
	private StaffService staffService;
	
	@Override
	public User doSignIn(LoginForm loginForm) {
		
		return userDao.isLoginSuccess(loginForm);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public User populateUserDetails(User user) {
		User derivedUser = null;
		
		if(user.getUserType() == UserType.STUDENT.getUserType()) {
			Student student = studentService.getStudent(user.getReferenceId());
			//TODO: Do something
			copyUserDetails(user, student);
/*			student.setUserName(user.getUserName());
			student.setUserType(user.getUserType());
			student.setReferenceId(user.getReferenceId());*/
			
			derivedUser = student;
		}
		else if(user.getUserType() == UserType.STAFF.getUserType()) {
			//log.info("The user object " + user);
			Staff staff = staffService.getStaff(user.getReferenceId());
			//TODO: Do something
			copyUserDetails(user, staff);
			
			derivedUser = staff;
			
		}
		else if(user.getUserType() == UserType.ADMIN.getUserType()) {
		
		}
		
		return derivedUser;	
	}
	
	private void copyUserDetails (User from, User to) {
		to.setUserName(from.getUserName());
		to.setUserType(from.getUserType());
		to.setReferenceId(from.getReferenceId());
	}
	
	@Override
	public boolean updateUserDetails(User user) {
		if(user.getUserType() == UserType.STUDENT.getUserType()) {
		return studentService.updateStudentInfo((Student)user);
		}
		if(user.getUserType() == UserType.STAFF.getUserType()) {
			return staffService.updateStaffInfo((Staff)user);
			}
		return false;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	@Autowired
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	@Autowired
	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

}
